package com.china_goldcard.igas.database.core;

import android.database.sqlite.SQLiteDatabase;

import com.china_goldcard.igas.core.AppContext;
import com.china_goldcard.igas.database.greendao.DaoMaster;
import com.china_goldcard.igas.database.greendao.DaoSession;
import com.china_goldcard.igas.utils.Constants;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * greenDao管理器类
 */
public class GreenDaoManager{

    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private static GreenDaoManager instance;

    /**
     * 私有的构造方法
     */
    private GreenDaoManager(){
        mHelper = new DaoMaster.DevOpenHelper(AppContext.getInstance(), Constants.DATABASE_NAME,null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
        if (Constants.isDebug){
            QueryBuilder.LOG_SQL = true;
            QueryBuilder.LOG_VALUES = true;
        }

    }

    /**
     * 获取实体类
     * @return
     */
    public static GreenDaoManager getInstance(){
        if (instance == null) {
            synchronized (GreenDaoManager.class) {
                if (instance == null) {
                    instance = new GreenDaoManager();
                }
            }
        }
        return instance;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public DaoSession getmDaoSession() {
        return mDaoSession;
    }
}
